package com.troy.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.troy.entity.User;
import com.troy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Description
 * @Author: zhangh
 * @Date: 2020/12/22 11:21
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/save")
    public void save(@RequestBody User user){
        userService.save(user);
    }

    @GetMapping("/queryByid/{id}")
    public User queryByid(@PathVariable("id")Integer id){
        return userService.queryByid(id);
    }

    @GetMapping("/query")
    public String query() throws JsonProcessingException {
        List<User> users = userService.selectAll();
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(users);
        System.out.println("对象转为字符串"+json);
        System.out.println("========================");
        System.out.println(users);
        return users.toString();
    }

    @PutMapping("/update")
    public void update(@RequestBody User user){
        userService.update(user);
    }

    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") String id){
        userService.delete(id);
    }

    @GetMapping("test1/{id}")
    public User test1(@PathVariable("id")String id) throws JsonProcessingException {
        User user = userService.test1(id);
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(user);
        System.out.println("对象转为字符串"+json);
        System.out.println("========================");
        System.out.println(user);
        return user;
    }


}
    